package com.hisan.freeride.common.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.hisan.freeride.common.db.DBHelper;
import com.hisan.freeride.home.model.CitySite;

import static com.hisan.freeride.common.db.DBHelper.CITY_TOKEN;


/**
 * Created by Administrator on 2017/2/16.
 */

public class CityManger {
    private SQLiteDatabase database;
    private DBHelper helper;

    public CityManger(Context context){
        helper= DBHelper.getInstace(context);
    }
    /*
     * 添加昵称
     * ContentValues:相当于map
     */
    public void insert(CitySite user) {
        ContentValues values=new ContentValues();
        values.put("id",user.getId());
        values.put("city",user.getName());
        //获取sqlitedatabase实例
        database=helper.getWritableDatabase();
        //插入
        database.insert(CITY_TOKEN, null, values);
        close();
    }

    //查询所有用户
    public Cursor queryAllUser(){
        database=helper.getWritableDatabase();
        Cursor c=database.query(CITY_TOKEN, null, null, null, null, null, null, null);
        return c;
    }

    //删除表
    public void delete(){
        database=helper.getWritableDatabase();
        Cursor cursor = database.query(CITY_TOKEN, null, null, null, null, null, null);
        cursor.moveToFirst();
        if (cursor.getCount() > 0) {
            database.delete(CITY_TOKEN, null, null);
            close();
        }
    }

    /**
     * 方法1：检查某表列是否存在
     * @param columnName 列名
     * @return
     */
    public boolean checkColumnExist1(String columnName) {
        boolean result = false ;
        Cursor cursor = null ;
        database=helper.getWritableDatabase();
        try{
            //查询一行
            cursor = database.rawQuery( "SELECT * FROM " + CITY_TOKEN + " LIMIT 0"
                    , null );
            result = cursor != null && cursor.getColumnIndex(columnName) != -1 ;
        }catch (Exception e){
        }finally{
            if(null != cursor && !cursor.isClosed()){
                cursor.close() ;
            }
        }
        return result ;
    }



    /**
     * 清除表中所有数据
     */
    public void claer(){
        database=helper.getWritableDatabase();
        String sql="delete from wxcity where 1=1";
        database.execSQL(sql);
        close();
    }


    public void close() {
        helper.close();
    }
    /**
     * 判断某张表是否存在
     * @param
     * @return
     */
    public boolean tabIsExist(){
        boolean result = false;
        if(CITY_TOKEN == null){
            return false;
        }
        SQLiteDatabase db = null;
        Cursor cursor = null;
        try {
            database=helper.getWritableDatabase();
            String sql = "select count(*) as c from sqlite_master where type ='table' and name ='"+CITY_TOKEN.trim()+"' ";
            cursor = db.rawQuery(sql, null);
            while (cursor.moveToNext()){
                int count = cursor.getCount();
                if(count>0){
                    result = true;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            close();
            // TODO: handle exception
        }
        close();
        return result;
    }
}
